


// 合并 reducers
import { combineReducers } from 'redux'
// 导入类型
import { ADD, DELETE, CHANGE, SET_USER, CLEAR_USER } from './actionType'

// 待办事项的 reducer
const todoReducer = (state = [], action) => {
    switch (action.type) {
        case ADD:
            return [...state, action.payload]
        case DELETE:
            return state.filter(todo => todo !== action.payload)
        case CHANGE:
            return state.map(item => item === action.payload ? { ...item, done: !item.done } : item)
        default:
            return state
    }
}

// 用户信息的 reducer
const userReducer = (state = {}, action) => {
    switch (action.type) {
        case SET_USER:
            return action.payload
        case CLEAR_USER:
            return {}
        default:
            return state
    }
}

// 合并所有 reducers
const rootReducer = combineReducers({
    todos: todoReducer,
    user: userReducer
})

export default rootReducer

